package com.tjsinfo.tjsoa.system.bo;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.tjsinfo.tjsoa.Dao.IHibernateDao;
import com.tjsinfo.tjsoa.Dao.IQbcDao;
import com.tjsinfo.tjsoa.system.dao.ISystemSpringDao;
import com.tjsinfo.tjsoa.system.dao.ISystemUserDepartmentDao;
import com.tjsinfo.tjsoa.system.dao.ITSystemDepartmentDao;
import com.tjsinfo.tjsoa.system.vo.TSystemDepartment;
import com.tjsinfo.tjsoa.system.vo.TSystemUserDepartment;

public class SystemUserDepartmentBo implements ISystemUserDepartmentBo {
	private ISystemUserDepartmentDao systemUserDepartmentDao;
	private IHibernateDao   hibernateDao;
	private IQbcDao qbcDao;
	private ITSystemDepartmentDao  systemDepartmentDao;
	
	public ITSystemDepartmentDao getSystemDepartmentDao() {
		return systemDepartmentDao;
	}

	public void setSystemDepartmentDao(ITSystemDepartmentDao systemDepartmentDao) {
		this.systemDepartmentDao = systemDepartmentDao;
	}

	public IQbcDao getQbcDao() {
		return qbcDao;
	}

	public void setQbcDao(IQbcDao qbcDao) {
		this.qbcDao = qbcDao;
	}

	public IHibernateDao getHibernateDao() {
		return hibernateDao;
	}

	public void setHibernateDao(IHibernateDao hibernateDao) {
		this.hibernateDao = hibernateDao;
	}

	public ISystemUserDepartmentDao getSystemUserDepartmentDao() {
		return systemUserDepartmentDao;
	}

	public void setSystemUserDepartmentDao(
			ISystemUserDepartmentDao systemUserDepartmentDao) {
		this.systemUserDepartmentDao = systemUserDepartmentDao;
	}

	public Integer addSystemUserDepartment(
			TSystemUserDepartment systemUserDepartment) {
		// TODO Auto-generated method stub
		return hibernateDao.save(systemUserDepartment);
	}

	public Boolean deleteSystemUserDepartment(Integer systemUserDepartmentId) {
		// TODO Auto-generated method stub
		try {
			TSystemUserDepartment systemUserDepartment = selectById(systemUserDepartmentId);
			hibernateDao.delete(systemUserDepartment);
			return true;
		} catch (Exception e) {
			// TODO: handle exception
			return false;
		}
	}

	public TSystemUserDepartment selectById(Integer systemUserDepartmentId) {
		// TODO Auto-generated method stub
		return (TSystemUserDepartment) hibernateDao.findById(TSystemUserDepartment.class, systemUserDepartmentId);
	}

	public List<TSystemUserDepartment> selectByUserId(Integer userId) {
		// TODO Auto-generated method stub
		DetachedCriteria criteria = DetachedCriteria.forClass(TSystemUserDepartment.class);
		criteria.add(Restrictions.eq("userId", userId));
		List<TSystemUserDepartment> list = qbcDao.getCriteria(criteria);
		List<TSystemUserDepartment> systemuserdepartmentlist = new ArrayList<TSystemUserDepartment>();
		for (Iterator iterator = list.iterator(); iterator
				.hasNext();) {
			TSystemUserDepartment systemUserDepartment = (TSystemUserDepartment) iterator.next();
			systemUserDepartment.setDepartmentName(systemDepartmentDao.findById(systemUserDepartment.getDepartmentId()).getDeptName());
			systemuserdepartmentlist.add(systemUserDepartment);
		}
		return systemuserdepartmentlist;
	}

	public List<TSystemUserDepartment> selectByDepartmentId(Integer departmentId) {
		// TODO Auto-generated method stub
		DetachedCriteria criteria = DetachedCriteria.forClass(TSystemUserDepartment.class);
		criteria.add(Restrictions.eq("departmentId", departmentId));
		List<TSystemUserDepartment> list = qbcDao.getCriteria(criteria);
		List<TSystemUserDepartment> systemuserdepartmentlist = new ArrayList<TSystemUserDepartment>();
		for (Iterator iterator = list.iterator(); iterator
				.hasNext();) {
			TSystemUserDepartment systemUserDepartment = (TSystemUserDepartment) iterator.next();
			systemUserDepartment.setDepartmentName(systemDepartmentDao.findById(systemUserDepartment.getDepartmentId()).getDeptName());
			systemuserdepartmentlist.add(systemUserDepartment);
		}
		return systemuserdepartmentlist;
	}
	
	public Boolean deleteSystemUserDepartmentById(Integer userId) {
		// TODO Auto-generated method stub
		try {
			List<TSystemUserDepartment> list = selectByUserId(userId);
			for (TSystemUserDepartment systemUserDepartment : list) {
				deleteSystemUserDepartment(systemUserDepartment.getId());
			}
			return true;
		} catch (RuntimeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		
	}

}
